//
//  HSLotteryBallVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/30.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSLotteryBallVC.h"
#import "HSLotteryBallCell.h"
#import "HSLotteryBallDetailVC.h"

@interface HSLotteryBallVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<HSLotteryBallModel *> *models;

@end

@implementation HSLotteryBallVC

- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"竞彩";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.models = @[].mutableCopy;
    self.hintView.reloadBlock();
}

- (void)refresh
{
    NSDictionary *params = @{@"opencode_list": @{},@"c_head": @{
                                     @"client_id": @"BY003000000000000002",
                                     @"client_os": @"IOS"
                                     }};
    @weakify(self);
    [[MYHttpHelper shareHelper] postWithUrl:@"http://mycp.iplay78.com/trade-web/web/opencode_list" params:params success:^(NSDictionary *dict) {
        @strongify(self);
        NSDictionary *opencode_list = dict[@"opencode_list"];
        NSArray<HSLotteryBallModel *> *models = [NSArray modelArrayWithClass:[HSLotteryBallModel class] json:opencode_list[@"data"]];
        [models enumerateObjectsUsingBlock:^(HSLotteryBallModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.guest_team.length > 0) {
                [self.models addObject:obj];
            }
        }];
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 103;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSLotteryBallCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSLotteryBallCell class])];
    cell.model = self.models[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSLotteryBallModel *model = self.models[indexPath.row];
    [self my_push:[[HSLotteryBallDetailVC alloc] initWithLotId:model.lot_id lotName:model.lot_name]];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight - TabbarHeight)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;\
        [_tableView registerClass:[HSLotteryBallCell class] forCellReuseIdentifier:NSStringFromClass([HSLotteryBallCell class])];
    }
    return _tableView;
}

@end
